package com.seveneight.baijiesteward.views;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.bean.VersionBean;
import com.seveneight.baijiesteward.service.DownLoadService;
import com.seveneight.baijiesteward.ui.main.fragment.MainFragment;
import com.seveneight.baijiesteward.ui.user.fragment.ManagerAccountFragment;
import com.seveneight.baijiesteward.utils.LogUtil;
import com.seveneight.baijiesteward.utils.MMessageDialog;
import com.seveneight.baijiesteward.utils.ToastUtil;

/**
 * Created by user1 on 2018/4/2.
 */

public class UpdateDialog {
    private Activity context;

    public UpdateDialog(Activity context) {
        this.context = context;
    }

    public void needUpdateDialog(VersionBean versionBean) {
        new MaterialDialog.Builder(context)
                .title("更新提醒")
                .titleColorRes(R.color.graytext)
                .content(versionBean.getUpdateInfo())
                .contentColorRes(R.color.graytext)
                .backgroundColorRes(R.color.white)
                .negativeColorRes(R.color.mainblue)
                .negativeText(R.string.negative)
                .positiveColorRes(R.color.mainblue)
                .positiveText(R.string.positive)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                        Uri uri = Uri.parse(versionBean.getUpdateUrl());
//                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
//                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                        context.startActivity(intent);
                        Intent service = new Intent(context, DownLoadService.class);
                        LogUtil.d("downloadUrl" + versionBean.getUpdateUrl());
                        service.putExtra("downloadurl", versionBean.getUpdateUrl());
                        context.startService(service);
                        ToastUtil.show(context, "开始下载");
                    }
                })
                .build().show();
    }

    public void noUpdateDialog() {
        MMessageDialog dialog = new MMessageDialog(context, "已经是最新版本");
        dialog.showDialog();
    }
}
